
const http = require("http");
const db = require("./db");
const qs = require("querystring");
http.createServer((req,res)=>{
    res.setHeader("Access-Control-Allow-Origin", "*")//跨域
    res.setHeader('Content-Type', 'text/html; charset=utf-8');//设置乱码
    if(req.url=="/favicon.ico"){
        return;
    }
    console.log(req.url);
    let str = req.url.split("?")[1];
    let data = qs.parse(str);

    if(data.name == ""  || data.password == "" || data.name == undefined || data.password == undefined ){
        let data = {
            code:10001,
            err:'注册失败',
            msg:"用户名或者密码为输入"
        }
        res.end(JSON.stringify(data))
    }

    let sql  = `INSERT INTO user (id, name, password) VALUES (NULL, "${data.name}","${data.password}")`;
    db(sql,(err,result)=>{
        if(err){
            throw err;
        }
        let data = {
            code:200,
            msg:"注册成功",
            data:result
        }
        res.end(JSON.stringify(data))
    })

}).listen(3001)

console.log(3001);